ggplot2 - tutorial

library(ggplot2)

d <- data.frame(a=letters[1:10], b=1:10)

ggplot(d, aes(x=a, y=b)) + 
    geom_point()

# ciekawostka
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- ggplot(d, aes(x=a, y=b)) +
    geom_point()

ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
# ustawienie językowe dla polskich znaków przy eksporcie do pdf
Sys.setlocale(category="LC_CTYPE", locale="pl_PL.utf8")
## [1] "pl_PL.utf8"
# R for data science

library(tidyverse)
## ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble  1.4.2     ✔ purrr   0.2.4
## ✔ tidyr   0.8.0     ✔ dplyr   0.7.4
## ✔ readr   1.1.1     ✔ stringr 1.2.0
## ✔ tibble  1.4.2     ✔ forcats 0.2.0
## ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
    geom_jitter(width = 0.03)  # to samo, co geom_point(position = "jitter") 

# punktowe
ggplot(data=mpg) +
    geom_point(mapping = aes(x = displ, y = hwy, color = class))

    # alpha, shape, color albo size

ggplot(data = mpg) +
    geom_point(mapping = aes(x = displ, y = hwy)) +
    facet_wrap(~ drv, ncol=1)

ggplot(data = mpg) +
    geom_point(mapping = aes(x = displ, y = hwy)) +
    facet_grid(drv ~ cyl)  # facet_wrap, tylko na dwa wymiary

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
    geom_count(color = "blue") +  # na pokrywajÄ…ce siÄ™ dane (overplotting)
    geom_point(position = "jitter")  # można też dać geom_jitter(width=0.03)

    # liniowe
ggplot(data = mpg) + 
    geom_smooth(mapping = aes(x = displ, y = hwy, color = drv, linetype=drv)) +
    geom_point(mapping = aes(x = displ, y = hwy, color=drv))
## `geom_smooth()` using method = 'loess'

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
    geom_point(mapping = aes(color = class)) +
    geom_smooth()
## `geom_smooth()` using method = 'loess'

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
    geom_point(mapping = aes(color = drv), show.legend = F) +
    geom_smooth(data = filter(mpg, drv == "f")) +
    geom_smooth(data = filter(mpg, drv == "r"), se = F)  # standard error
## `geom_smooth()` using method = 'loess'
## `geom_smooth()` using method = 'loess'

# słupkowe
ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut))  # histogram liczebności

d <- data.frame(name=letters[1:5], value=sample(50:100, 5))
ggplot(data = d) +
    geom_bar(mapping = aes(x = name, y = value), stat = "identity")

ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut, fill = cut))

ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut, fill = clarity))

d <- data.frame(pk=c(rep('p', 4), rep('k', 4)), name=rep(letters[1:4], 2), value=sample(2:8, 8, replace=T))
ggplot(data = d, mapping = aes(x = pk, y = value, color = name)) +
    geom_bar(stat = "identity", fill = NA)

# boxplot
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
    geom_boxplot() +
    coord_flip()  # poziomy wykres

# układ współrzędnych
usa <- map_data("usa")
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
ggplot(usa, aes(long, lat, group = group)) +
    geom_polygon(fill = "white", color = "black") +
    coord_quickmap()  # skaluje współrzędne

# ogólny wygląd - dodatki
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
    geom_rect(mapping=aes(xmin=15, xmax=20, ymin=0, ymax=max(hwy)), 
              fill='blue', alpha=0.1) +
    geom_point() + 
    labs(title = "JakiÅ›\nwykres",
         subtitle = "subtitle to chart",
         caption = "and caption: made by me",
         x = "ąźćęłó city miles per gallon", 
         y = "highway miles per gallon") +
    geom_abline(color ="red") +  # a = 1, b = 0, są też geom_hline i geom_vline
    theme_bw() +  # prosty styl
    theme(plot.title = element_text(hjust = 0.5, size=12),
          axis.title = element_text(size=12))